<html>
<head><meta charset="utf-8"><title>const generics creating bound vars · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html">const generics creating bound vars</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236382178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/236382178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#236382178">(Apr 27 2021 at 18:02)</a>:</h4>
<p>I have been working on a pull request (<a href="https://github.com/rust-lang/rust/issues/83825">#83825</a>) to remove <code>Binder::bind</code> and only use <code>Binder::dummy</code> to try to overcome some of the performance regressions from the binder bound variable refactor. The whole testsuite passes, except for one test involving const generics, and my reviewer (<span class="user-mention silent" data-user-id="232957">Jack Huey</span>) isn't familiar enough with const generics to know whether it's expected that the code is creating bound variables.</p>
<p>The test that's failing is the <code>full</code> revision of <code>src/test/ui/const-generics/issues/issue-64494.rs</code>. I wrote up what I could figure out about what's going on here: <a href="https://github.com/rust-lang/rust/pull/83825#issuecomment-815423610">https://github.com/rust-lang/rust/pull/83825#issuecomment-815423610</a>.</p>
<p><span class="user-mention" data-user-id="216206">@lcnr</span> do you know if it's expected that the code is creating bound variables?</p>
<p>cc <span class="user-mention" data-user-id="232957">@Jack Huey</span> in case you want to follow along</p>



<a name="236382249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/236382249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#236382249">(Apr 27 2021 at 18:02)</a>:</h4>
<p>This is the content of the test:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="c1">// revisions: full min</span>
<span class="cp">#![cfg_attr(full, feature(const_generics))]</span><span class="w"></span>
<span class="cp">#![cfg_attr(full, allow(incomplete_features))]</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">VAL</span>: <span class="kt">usize</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">MyTrait</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">True</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Is</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">T</span>: <span class="kt">bool</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">True</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Is</span><span class="o">&lt;</span><span class="p">{</span><span class="kc">true</span><span class="p">}</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">Foo</span><span class="o">&gt;</span><span class="w"> </span><span class="n">MyTrait</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">Is</span><span class="o">&lt;</span><span class="p">{</span><span class="n">T</span>::<span class="n">VAL</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="p">}</span><span class="o">&gt;</span>: <span class="nc">True</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="c1">//[full]~^ ERROR constant expression depends on a generic parameter</span>
<span class="c1">//[min]~^^ ERROR generic parameters may not be used in const operations</span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">Foo</span><span class="o">&gt;</span><span class="w"> </span><span class="n">MyTrait</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">Is</span><span class="o">&lt;</span><span class="p">{</span><span class="n">T</span>::<span class="n">VAL</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">6</span><span class="p">}</span><span class="o">&gt;</span>: <span class="nc">True</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="c1">//[full]~^ ERROR constant expression depends on a generic parameter</span>
<span class="c1">//[min]~^^ ERROR generic parameters may not be used in const operations</span>
<span class="c1">//[min]~| ERROR conflicting implementations of trait `MyTrait`</span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>



<a name="236382344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/236382344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#236382344">(Apr 27 2021 at 18:03)</a>:</h4>
<p>The test ICEs after my change, but if I remove either conflicting impl, the test passes!</p>



<a name="236382500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/236382500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#236382500">(Apr 27 2021 at 18:04)</a>:</h4>
<p>The place that's ICEing is <a href="https://github.com/rust-lang/rust/blob/61efe217f48b22ab8984eb9891955d87e029d65c/compiler/rustc_ty_utils/src/instance.rs#L118">this line</a> that I changed from <code>Binder::bind</code> to <code>Binder::dummy</code> (the link shows what it looks like after my change).</p>



<a name="236411012"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/236411012" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#236411012">(Apr 27 2021 at 21:14)</a>:</h4>
<p>does this ICE happen while const evaluating one of the <code>Is&lt;expr&gt;</code> expressions?</p>



<a name="236411170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/236411170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#236411170">(Apr 27 2021 at 21:15)</a>:</h4>
<p>i would assume this to not use a bound var <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="236411312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/236411312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#236411312">(Apr 27 2021 at 21:15)</a>:</h4>
<p><code>T</code> should probably be a placeholder instead, though i can't say more without looking into this myself</p>



<a name="236417316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/236417316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#236417316">(Apr 27 2021 at 22:01)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/const.20generics.20creating.20bound.20vars/near/236411012">said</a>:</p>
<blockquote>
<p>does this ICE happen while const evaluating one of the <code>Is&lt;expr&gt;</code> expressions?</p>
</blockquote>
<p>I think maybe, because the ICE happens right after <code>resolve_associated_item(Foo::VAL)</code>.</p>



<a name="236417522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/236417522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Huey <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#236417522">(Apr 27 2021 at 22:03)</a>:</h4>
<p>Just to clarify: the ICE happens <em>because</em> there are anonymous bound regions that we don't keep track of</p>



<a name="236418287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/236418287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#236418287">(Apr 27 2021 at 22:11)</a>:</h4>
<p>I was kind of wondering, why <em>is</em> it okay to get rid of <code>Binder::bind</code> given that its job is to track the bound variables?</p>



<a name="236446114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/236446114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Huey <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#236446114">(Apr 28 2021 at 03:52)</a>:</h4>
<p>Well, we really should only be using <code>bind_with_vars</code> (when we know what the bound vars are) and <code>dummy</code> (when there are no bound vars). We should <em>not</em> be in situations where there are bound vars but we don't know what they are</p>



<a name="236741861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/236741861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#236741861">(Apr 29 2021 at 20:43)</a>:</h4>
<p>Ok, thanks! I get it now.</p>



<a name="238228491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/238228491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Santiago Pastorino <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#238228491">(May 10 2021 at 23:44)</a>:</h4>
<p><span class="user-mention" data-user-id="307537">@Camelid</span> are you currently pursuing this? <span class="user-mention" data-user-id="232957">@Jack Huey</span> asked me if I could help out</p>



<a name="238228506"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/238228506" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Santiago Pastorino <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#238228506">(May 10 2021 at 23:44)</a>:</h4>
<p>more than happy to try to help with things</p>



<a name="238228520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/238228520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Santiago Pastorino <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#238228520">(May 10 2021 at 23:44)</a>:</h4>
<p>or even try to take over if you do not want to continue</p>



<a name="238397288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/238397288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#238397288">(May 11 2021 at 23:27)</a>:</h4>
<p>Thanks for the offer, help is appreciated!</p>



<a name="238397412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/238397412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#238397412">(May 11 2021 at 23:29)</a>:</h4>
<p>The issue is just that I am unfamiliar with a lot of the type-checking code, and so I feel confused about why a bound variable is showing up.</p>



<a name="238397577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/238397577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#238397577">(May 11 2021 at 23:30)</a>:</h4>
<p>So, if you could help me figure out why that's happening and what it should do instead, that would be helpful :)</p>



<a name="238397602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/238397602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#238397602">(May 11 2021 at 23:31)</a>:</h4>
<p>Part of why I took this change on is that I was hoping to become more familiar with rustc's type checker and trait solver :)</p>



<a name="238408850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/238408850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Huey <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#238408850">(May 12 2021 at 01:49)</a>:</h4>
<p>Yeah, this perhaps isn't the best issue for that lol</p>



<a name="238409000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/238409000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Huey <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#238409000">(May 12 2021 at 01:50)</a>:</h4>
<p>Honestly, there are two possible solutions here: 1) change something so that we <em>don't</em> have bound vars where we're ICEing (it's in an <code>Instance</code>, which AFAICT is a no-no anyways) 2) Specifically track the bound vars all the from the type checking or whatever that generates this</p>



<a name="238409023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/const%20generics%20creating%20bound%20vars/near/238409023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Huey <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/const.20generics.20creating.20bound.20vars.html#238409023">(May 12 2021 at 01:51)</a>:</h4>
<p>Either way requires us to essentially to followup that "tree" back up and find the originating code for the bound vars. Which is where I got stuck/ran out of time</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>